Slip 5

Q.1. Write a python program to implement Multiple Linear Regression for Fuel Consumption 
dataset.

# multiple_linear_regression_fuel_consumption.py

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# ------------------------------
# Step 1: Create a Sample Fuel Consumption Dataset
# ------------------------------
data = {
    'Engine_Size': [1.6, 2.0, 2.4, 3.5, 3.0, 4.0, 1.8, 2.2, 3.2, 3.6],
    'Cylinders': [4, 4, 4, 6, 6, 8, 4, 4, 6, 6],
    'Fuel_Consumption_City': [8.5, 9.0, 9.5, 11.2, 10.5, 12.5, 8.8, 9.8, 10.8, 11.5],
    'Fuel_Consumption_Hwy': [6.0, 6.5, 7.0, 8.0, 7.5, 9.0, 6.2, 6.8, 7.8, 8.2],
    'CO2_Emissions': [196, 221, 240, 260, 250, 300, 200, 230, 255, 270]
}

df = pd.DataFrame(data)
print("Fuel Consumption Dataset:")
print(df, "\n")

# ------------------------------
# Step 2: Select Features and Target Variable
# ------------------------------
# Features (independent variables)
X = df[['Engine_Size', 'Cylinders', 'Fuel_Consumption_City', 'Fuel_Consumption_Hwy']]

# Target (dependent variable)
y = df['CO2_Emissions']

# ------------------------------
# Step 3: Split Data into Training and Testing Sets
# ------------------------------
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# ------------------------------
# Step 4: Train the Multiple Linear Regression Model
# ------------------------------
model = LinearRegression()
model.fit(X_train, y_train)

# ------------------------------
# Step 5: Make Predictions
# ------------------------------
y_pred = model.predict(X_test)

# ------------------------------
# Step 6: Evaluate Model Performance
# ------------------------------
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print("Multiple Linear Regression Model Results")
print("---------------------------------------")
print(f"Intercept: {model.intercept_:.2f}")
print(f"Coefficients: {list(zip(X.columns, model.coef_))}")
print(f"Mean Squared Error (MSE): {mse:.2f}")
print(f"R² Score: {r2:.4f}\n")

# ------------------------------
# Step 7: Compare Actual vs Predicted Values
# ------------------------------
results = pd.DataFrame({'Actual': y_test.values, 'Predicted': y_pred})
print("Actual vs Predicted CO2 Emissions:")
print(results, "\n")

# ------------------------------
# Step 8: Visualization
# ------------------------------
plt.figure(figsize=(6, 5))
plt.scatter(y_test, y_pred, color='blue')
plt.plot([y.min(), y.max()], [y.min(), y.max()], color='red', linewidth=2)
plt.xlabel('Actual CO2 Emissions')
plt.ylabel('Predicted CO2 Emissions')
plt.title('Actual vs Predicted CO2 Emissions (Multiple Linear Regression)')
plt.show()   

Q.2. Write a python program to implement k-nearest Neighbors ML algorithm to build 
prediction model (Use iris Dataset).

# Import necessary libraries
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

# Step 1: Load the Iris dataset
iris = load_iris()
X = iris.data      # Features: sepal length, sepal width, petal length, petal width
y = iris.target    # Target: 0 - setosa, 1 - versicolor, 2 - virginica

# Step 2: Convert into a pandas DataFrame (optional, for visualization)
df = pd.DataFrame(X, columns=iris.feature_names)
df['Target'] = y
print("Iris Dataset (first 5 rows):\n", df.head(), "\n")

# Step 3: Split dataset into training and testing parts (80% train, 20% test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Step 4: Create and train the KNN model
k = 3  # You can change this value for experimentation
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)

# Step 5: Predict on test data
y_pred = knn.predict(X_test)

# Step 6: Evaluate model performance
print("Predictions:", y_pred)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nConfusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))

# Step 7: Example prediction
sample = [[5.1, 3.5, 1.4, 0.2]]  # Sepal length, Sepal width, Petal length, Petal width
predicted_class = knn.predict(sample)
print("\nExample Input:", sample)
print("Predicted Class:", iris.target_names[predicted_class][0])